/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

#include "ConnectionListenerTester.hpp"

ConnectionListenerTester::ConnectionListenerTester(): m_poReceiveBuffer(NULL) {
	reset();
}

ConnectionListenerTester::~ConnectionListenerTester() {

	notifyWaiter();
	delete m_poReceiveBuffer;
}

void ConnectionListenerTester::onConnected(IConnection* poConnection) throw() {
	m_bConnected = true;
	notifyWaiter();
}

void ConnectionListenerTester::onDisconnected(IConnection* poConnection) throw() {
	m_bConnected = false;
	notifyWaiter();
}

void ConnectionListenerTester::onReceived(IConnection* poConnection, cpputils::io::ByteBuffer* poBuffer) throw() {

	if(m_poReceiveBuffer!=NULL)
		delete m_poReceiveBuffer;
	m_poReceiveBuffer = new ByteBuffer(poBuffer->getLen());
	m_poReceiveBuffer->append(*poBuffer);
	++this->m_nReceiveEventCounter;
	notifyWaiter();
}

const unsigned ConnectionListenerTester::getReceiveEventCounter(){
	return this->m_nReceiveEventCounter;
}

ByteBuffer* ConnectionListenerTester::getLastReceivedMessage() {
	return m_poReceiveBuffer;
}

void ConnectionListenerTester::onConnectionFailure(IConnection* poConnection, const std::string& sErrorMessage) throw() {
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiter();
}

void ConnectionListenerTester::onReceiveError(IConnection* poConnection, const std::string& sErrorMessage) throw() {
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiter();
}

void ConnectionListenerTester::onWriteError(IConnection* poConnection, const std::string& sErrorMessage) throw() {
	m_sLastErrorMessage = sErrorMessage;
	notifyWaiter();
}


bool ConnectionListenerTester::isConnected() {
	return m_bConnected;
}

std::string ConnectionListenerTester::getLastErrorMessage() {
	return m_sLastErrorMessage;
}

void ConnectionListenerTester::reset() {
	m_nReceiveEventCounter = 0;
	m_sLastErrorMessage = "";
	m_bConnected = false;
	//m_evAwait.notify_all();
}

boost::mutex& ConnectionListenerTester::getMonitor() {
	return m_mxMonitor;
}

void ConnectionListenerTester::waitForAnyEvent(boost::mutex::scoped_lock& oLock, const std::string& sLocation /*= "Unknown location"*/) {
	boost::xtime oXt;
	boost::xtime_get(&oXt, boost::TIME_UTC);
	oXt.sec += 10; // 10 sec is max await time
	if(!m_evAwait.timed_wait(oLock,oXt)) {
		std::cout << "Timeout for waiting connection event! Location: " << sLocation <<std::endl;
		throw SystemException("Timeout for waiting connection event! Location: " + sLocation);
	}
}

void ConnectionListenerTester::notifyWaiter() {
	boost::mutex::scoped_lock oLock(m_mxMonitor);
	m_evAwait.notify_one();
}


